import { request } from "../../request/index";
import "../../lib/running/runtime";
Page({
  /**
   * 页面的初始数据
   */
  data: {
    tabs: [
      {
        value: "综合",
        id: 1,
        isActive: true,
      },
      {
        value: "销量",
        id: 2,
        isActive: false,
      },
      {
        value: "待付款",
        id: 3,
        isActive: false,
      },
    ],
    goods_list: [],
    cid:0,
    pageTotal:0,
    query:''
  },
  queryRequest:{
    pagesize:10,
    pagenum:1,
  },

    tabClickChange(e){
      // 点击下标
      const index = e.detail
      let _tabs = this.data.tabs;
      console.log(_tabs)
   _tabs.forEach(Item=>Item.id==index?Item.isActive=true:Item.isActive = false)
      this.setData({
        tabs:_tabs
      })
    },
// 数据请求
    async getGoodsList({query,cid}){
      // 数据加载动画
      // 请求参数获取
      if(query){
          this.queryRequest.query = query
      }else{
        this.queryRequest.cid = cid
      }
      let _res  = await request({
        url:"/goods/search",
        data:this.queryRequest
      })
    // 获取商品信息
      let {message}  = _res.data
      // 原商品信息
      let {goods_list} = this.data
      this.queryRequest.pagenum+=1
      this.setData({
        goods_list: [...goods_list,...message.goods],
        pageTotal: Math.ceil(message.total/this.queryRequest.pagesize)
      })
    },

    
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      cid:options.cid,
      query: options.query
    })
    this.getGoodsList({query:options.query,cid: options.cid})
  },


  onPullDownRefresh: function () {
    console.log("Test")
    // 重置数据
    this.setData({goods_list:[]})
    // 重新发请求
    this.queryRequest.pagenum = 0
    this.getGoodsList()
    wx.stopPullDownRefresh()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let {pageTotal,cid,query} = this.data
    if(pageTotal< this.queryRequest.pagenum){
      console.log("没有更多")
      wx.showToast({
        title:"没有更多"
      })
    }else{
      console.log("我发请求")
      this.getGoodsList({cid,query})
    }
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
});
